package com.store.bookstore_backend.dto;

import com.store.bookstore_backend.exception.BookStoreException;
import lombok.Data;

@Data
public class APIResponse<T> {
    private int code;           // 状态码
    private String message;     // 返回信息
    private T data;             // 返回的数据


    public APIResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 静态工厂方法
    public static <T> APIResponse<T> success(T data) {
        return new APIResponse<>(200, "ok", data);
    }

    public static <T> APIResponse<T> success() {
        return success(null);
    }

    public static <T> APIResponse<T> error(String message) {
        return new APIResponse<>(500, message, null);
    }

    public static <T> APIResponse<T> error(int code, String message) {
        return new APIResponse<>(code, message, null);
    }

    public static <T> APIResponse<T> error(BookStoreException.CodeMsgEnum msgEnum) {
        return new APIResponse<>(msgEnum.getCode(), msgEnum.name(), null);
    }

}

